/**
 * Copyright @ 2023 iAuto (Shanghai) Co., Ltd.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are NOT permitted except as agreed by
 * iAuto (Shanghai) Co., Ltd.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

/**
 * @description 权限按钮
 */
import { Button, ButtonProps } from 'antd';
import React, { ReactNode, CSSProperties } from 'react';
import { connect } from 'dva';

interface IProps extends ButtonProps {
  children: ReactNode;
  className?: string;
  style?: CSSProperties;
  auth?: string;
  pathname?: string;
  menu?: {
    permissions: string[];
  };
  buttonType: 'form' | 'table';
  type: undefined | 'primary' | 'link' | 'text';
}

const Index = ({
  children,
  menu,
  pathname,
  auth,
  buttonType = 'table',
  type,
  className = '',
  disabled,
  style,
  ...resProps
}: IProps) => {
  const render = () => {
    const res = menu?.permissions?.includes?.(pathname + '/' + auth);

    return buttonType === 'table' ? (
      <Button
        className={className}
        style={style}
        disabled={!res || disabled}
        type={'primary'}
        ghost
        size={'small'}
        {...resProps}
      >
        {children}
      </Button>
    ) : (
      <Button
        className={className}
        style={style}
        disabled={!res || disabled}
        type={type}
        size={'small'}
        {...resProps}
      >
        {children}
      </Button>
    );
  };
  return render();
};

export default connect((state: any) => ({
  menu: state.menu,
}))(Index);
